<?php
/**
* http://php.net/manual/en/function.curl-setopt.php
*/
class Curl {
    public $url = '';
    public $useAuth = ''; // for authenticate | 'username:password'
    public $sslVerify = FALSE;
    
    public $login_url = '';
    public $login_username = '';    // usernameField=username
    public $login_password = '';    // passwordField=password
    
    public $posts = array();
    
    public $headers = array(
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language: en-us,en;q=0.5",
        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
        "Keep-Alive: 115",
        "Connection: keep-alive",
    );
    public $cookie          = ''; // set cookie: fruit=apple; colour=red
    public $remoteIp        = '';
    public $proxy           = '';   // ip:port
    public $referer         = '';
    public $userAgent       = '';
    public $includeHeader   = FALSE;
    public $noBody          = FALSE;
    
    // after run
    public $content             = NULL;
    public $status              = NULL;
    
    public function setRandomSearchEngineUserAgent($userAgents = array())
    {
        if(!$userAgents){
            $userAgents = array(
                'Google - Googlebot/2.1 ( http://www.googlebot.com/bot.html)',
                'Google Image - Googlebot-Image/1.0 ( http://www.googlebot.com/bot.html)',
                'MSN Live - msnbot-Products/1.0 (+http://search.msn.com/msnbot.htm)',
                'Yahoo - Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)',
            );
        }
        $randKey = array_rand($userAgents, 1);
        $this->userAgent = $userAgents[$randKey];
    }
    
    
    public function __construct($url = ''){
        $this->url = $url;
    }
    
    public function run($url = NULL)
    {
        $url = $url ? $url : $this->url;

        $ch = curl_init();
        
        if($this->useAuth){
            curl_setopt($s, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
            curl_setopt($s, CURLOPT_USERPWD, $this->useAuth);
        }
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->sslVerify);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        
        if($this->remoteIp){
            $this->headers = array_merge($this->headers, array(
                "REMOTE_ADDR: {$this->remoteIp}", 
                "HTTP_X_FORWARDED_FOR: {$this->remoteIp}"
            ));
        }
        if($this->headers)          curl_setopt($ch,CURLOPT_HTTPHEADER, $this->headers); 
        if($this->proxy)            curl_setopt($ch,CURLOPT_PROXY,$this->proxy); 
        if($this->referer)          curl_setopt($ch,CURLOPT_REFERER,$this->referer);
        if($this->userAgent)        curl_setopt($ch,CURLOPT_USERAGENT,$this->userAgent);
        
        if($this->includeHeader)    curl_setopt($ch,CURLOPT_HEADER,true);
        if($this->noBody)           curl_setopt($ch,CURLOPT_NOBODY,true);
        
        if($this->cookie)           curl_setopt($ch,CURLOPT_COOKIE, $this->cookie);           
          
        if($this->login_url){
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_URL, $this->login_url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "{$this->login_username}&{$this->login_password}");
            //fopen('curl_cookie.txt', 'w');
            //echo is_writable(realpath('curl_cookie.txt')) ? 'writable' : 'not writable'; die;
            curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('curl_cookie.txt'));
            curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('curl_cookie.txt')); 
            curl_exec($ch);
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        if($this->posts){
            if(is_array($this->posts)){
                $this->posts = http_build_query($this->posts, FALSE, '&');
            }
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->posts);
        }

        $this->content  = curl_exec($ch);
        $this->status   = curl_getinfo($ch,CURLINFO_HTTP_CODE); 
        curl_close($ch);
        
        return $this->content;
    }
}



